﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace 计科201518彭康
{
    public partial class 实验2_3_1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["user"] == null)
            {
                this.Response.Redirect("实验2-3.aspx");
            }
            if (!IsPostBack)
            {
                Label1.Text = "欢迎你：" + this.Session["user"].ToString() + "，聊的开心哦!";
                Label2.Text = Application["VisitNumber"].ToString(); //提示在线人数
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //发言按钮
            //拥护+当前时间+吼+文本内容
            string chat = this.Session["user"].ToString() + "(" + DateTime.Now.ToString() + ")吼:" + TextBox2.Text;
            this.ListBox1.Items.Add(chat); //每发言一次，当前行加一条数据
            this.Application.Lock();
            string cChat = "";
            if (this.Application["chat"] != null)
            {
                cChat = this.Application["chat"].ToString();
            }
            this.Application["chat"] = cChat + "," + chat + ","; //将数据保存
            this.Application.UnLock();

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            this.ListBox1.Items.Clear();
            this.Application.Lock();
            this.Application["chat"] = ""; //将Application的内容赋值为空
            this.Application.UnLock();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            this.ListBox1.Items.Clear();
            if (this.Application["chat"] != null)
            {
                string[] arr = this.Application["chat"].ToString().Split(','); //将数据放到arr数组里，用逗号作为分隔符，逗号不计入数组
                foreach (string item in arr)
                {
                    if (item == "")
                    {
                        continue;
                    }
                    this.ListBox1.Items.Add(item);
                }
            }
        }
    }
}